//
//  SettingViewController.m
//  PlayBall
//
//  Created by Edward Zhang on 12-5-8.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "JddSettingViewController.h"
#import "JddCommonFunction.h"
#import "JddViewControllerManager.h"
#import "JddUserProfile.h"
#import "JddGlobal.h"


@interface JddSettingViewController ()

@end

@implementation JddSettingViewController
@synthesize listTable;

SHARED_VIEW_CONTROLLER_IMPLEMENT(JddSettingViewController);

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if ([alertView.message isEqualToString:@"确定退出登录？"]) {
        if (buttonIndex==0) {
            [JddUserProfile clearMessage];
            [[JddPickNum11s5ListViewController sharedInstance] destoryView];
            [[JddPickNum11s5ViewController sharedInstance] destoryView];
            [[JddAccountDetailViewController sharedInstance] destoryView];
            [[JddUCBuyHistoryViewController sharedInstance] destoryView];
            [[JddFollowNumberHistoryViewController sharedInstance] destoryView];
            [[JddPickNumDaLeTouListViewController sharedInstance] destoryView];
            [[JddPickNumDaLeTouViewController sharedInstance] destoryView];
            [[JddPickNumShuangSeQiuListViewController sharedInstance] destoryView];
            [[JddPickNumShuangSeQiuViewController sharedInstance] destoryView];
            [[JddPickNumSportListViewController sharedInstance] destoryView];
            
//            [[JddViewControllerManager sharedInstance] pushViewControllerWithIndex:CONTROLLER_USER_CENTER fromController:self withObject:nil withObject2:nil];
            
            [JddViewControllerManager sharedInstance].hideBackItem = YES;
            [[JddViewControllerManager sharedInstance] pushViewControllerWithIndex:CONTROLLER_LOGIN
                                                                    fromController:self
                                                                        withObject:nil
                                                                       withObject2:nil];
        }
    }
    else
        if ([alertView.message isEqualToString:@"确定退出程序？"]) {
            if (buttonIndex==0) {
                [JddUserProfile clearMessage];
                [[JddPickNum11s5ListViewController sharedInstance] destoryView];
                [[JddPickNum11s5ViewController sharedInstance] destoryView];
                [[JddAccountDetailViewController sharedInstance] destoryView];
                [[JddUCBuyHistoryViewController sharedInstance] destoryView];
                [[JddFollowNumberHistoryViewController sharedInstance] destoryView];
                [[JddPickNumDaLeTouListViewController sharedInstance] destoryView];
                [[JddPickNumDaLeTouViewController sharedInstance] destoryView];
                [[JddPickNumShuangSeQiuListViewController sharedInstance] destoryView];
                [[JddPickNumShuangSeQiuViewController sharedInstance] destoryView];
                [[JddPickNumSportListViewController sharedInstance] destoryView];
                
                [[JddViewControllerManager sharedInstance].navigationController setViewControllers:nil];
                exit(0);
                
            }
        }
        else if (([alertView.message isEqualToString:@"您使用的已是最新版本"])){
        }
        else
            /*if ([alertView.title isEqualToString:@"有新的版本可供升级，确定升级？"])*/ {
                if (buttonIndex == 0) {
                    NSString * URLString = @"https://itunes.apple.com/cn/app/jiang-duo-duo-cai-piao/id562188850?mt=8";//@"http://itunes.apple.com/cn/app/idNHQYR86CP9.com.jiangduoduo.jdd?mt=8";
                    if ([downLoadURL length] > 0) {
                        URLString = downLoadURL;
                    }
                    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:URLString]];
                }
            }
}
//检查版本回调
-(void)QueryVersionOperationResult:(NSData *)result{
    if (result) {
        
        int versionCode = [[[NSBundle mainBundle].infoDictionary objectForKey:@"CFBundleVersion"] intValue];
        //        int buildVersion= [[[NSBundle mainBundle].infoDictionary objectForKey:@"CFBundleShortVersionString"] intValue];
        
        SBJsonParser *jsonParser = [[SBJsonParser alloc] init];
        @try {
            NSDictionary *dictResult = [jsonParser objectWithData:result];
            int code = [[dictResult objectForKey:@"Code"] intValue];
            if (code == 0) {
                NSArray *temoArray = [jsonParser objectWithString:[dictResult objectForKey:@"Data"]];
                NSDictionary *dictResult22 = [temoArray objectAtIndex:0];
                int newCode = [[dictResult22 objectForKey:@"verCode"] intValue];
                //                int bupdate = [[dictResult22 objectForKey:@"bupdate"] intValue];
                newMessage = [dictResult22 objectForKey:@"Message"];
                downLoadURL = [dictResult22 objectForKey:@"apkname"];
                
                NSString *msg = @"有新的版本可供升级，确定升级？";
                for (NSString *inmsg in newMessage) {
                    msg = [msg stringByAppendingFormat:@"\n%@",inmsg];
                }
                
                if (versionCode < newCode /*&& bupdate == 1*/) {
                    UIAlertView *alert= [[UIAlertView alloc] initWithTitle:@"温馨提示" message:msg delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
                    [alert show];
                }else{
                    UIAlertView *alert= [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"您使用的已是最新版本" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                    [alert show];
                }
            }
        }
        @catch (NSException *e) {
            NSLog(@"NSException:\t%@", e.reason);
            [JddError sharedInstance].message = e.reason;
        }
        
    }
}
//退出登录 or 退出系统 or 检查版本
-(void)alerttype:(NSInteger)type{
    if (type==0) {
        UIAlertView *alert= [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"确定退出登录？" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
        [alert show];
    }else  if (type==1){
        UIAlertView *alert= [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"确定退出程序？" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
        [alert show];
    } else if (type==2) {
        JddQueryVersionOperation *operation = [[JddQueryVersionOperation alloc] init];
        operation.delegate = self;
        [[JddGlobalParameter sharedInstance].operationQueue addOperation:operation];
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.row) {
//        case 0://开奖通知
//            NSLog(@"开奖通知");
//            break;
        case 0://建议反馈
            [[JddViewControllerManager sharedInstance] pushViewControllerWithIndex:CONTROLLER_BACK fromController:self withObject:self withObject2:self];
            break;
        case 1://在线客服
        {
//            NSString *urlStr = @"http://www16.53kf.com/webCompany.php?arg=10129326";
            NSString *urlStr = @"http://www16.53kf.com/webCompany.php?arg=10129326&style=1&language=cn";
//            [[JddViewControllerManager sharedInstance] pushViewControllerWithIndex:CONTROLLER_MORE_CUSTOMER_SERVICEl fromController:self withObject:urlStr withObject2:nil];
            JddCustomerServiceViewController *custom = [[JddCustomerServiceViewController alloc] init];
            custom.webUrlString = urlStr;
            [self.navigationController pushViewController:custom animated:YES];
        }
            break;
        case 2://帮助
            [[JddViewControllerManager sharedInstance] pushViewControllerWithIndex:CONTROLLER_HOW_TO_PLAY_SSQ fromController:self withObject:nil withObject2:nil];

            break;
        case 3://关于我们
            [[JddViewControllerManager sharedInstance] pushViewControllerWithIndex:CONTROLLER_CONTACT_US fromController:self withObject:nil withObject2:nil];            break;
//        case 5://邀请好友
//            NSLog(@"邀请好友");
//
//            break;
        default:
            break;
    }
}
- (IBAction)backLoginBtnClicked:(UIButton *)sender
{
    [self alerttype:0];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 58;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return [titleArr count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    JddNewListViewCell * cell = [JddNewListViewCell getInstanceView];
    cell.delegate = self;
    cell.imageIcon.image = [UIImage imageNamed:[imageIconArr objectAtIndex:indexPath.row]];
    cell.titleLabel.text = [titleArr objectAtIndex:indexPath.row];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)dealloc
{
    NSLog(@"RootView");
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    UILabel *customLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 40, 30)];
    customLab.backgroundColor = [UIColor clearColor];
    [customLab setText:PAGE_USERCENTER_MORE];
    customLab.textAlignment = NSTextAlignmentCenter;
    [customLab setTextColor:[UIColor whiteColor]];
    customLab.font = [UIFont systemFontOfSize:19];
    self.navigationItem.titleView = customLab;
       
    
    
//    titleArr = [NSArray arrayWithObjects:@"开奖通知设置",@"建议反馈", @"在线客服",@"帮助",@"关于我们",@"邀请好友",nil];
    titleArr = [NSArray arrayWithObjects:@"建议反馈", @"在线客服",@"帮助",@"关于我们",nil];

//    imageIconArr = [NSArray arrayWithObjects:@"setting_kaijiangInfo.png",@"setting_fankui.png",@"setting_zaixiankefu.png",@"setting_help.png",@"setting_about_us.png",@"setting_invite_friend.png", nil];
    imageIconArr = [NSArray arrayWithObjects:@"setting_fankui.png",@"setting_zaixiankefu.png",@"setting_help.png",@"setting_about_us.png", nil];

    if (SCREEN_HEIGHT > 480) {
        CGFloat yPos = self.backLoginBtn.frame.origin.y - 20;
        self.backLoginBtn.frame = CGRectMake(self.backLoginBtn.frame.origin.x, yPos, self.backLoginBtn.frame.size.width, self.backLoginBtn.frame.size.height);
    }
}
- (void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void) viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"More"];

    //隐藏tabbar
    [self setJddTabBarHidden:YES];
    
    if ([JddCommonFunction isLogin]) {
        self.backLoginBtn.hidden = NO;
    } else {
        self.backLoginBtn.hidden = YES;
    }
    
    [listTable reloadData];
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"More"];
}
@end
